package com.hunteron.jdbc;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.hunteron.util.PropertieUtil;
import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 该项目用的连接池，通过该类简单实现，没有集成到spring中
 * @author Smile.Wu
 *
 */
public class JDBCConnector {
	private static Logger log = Logger.getLogger(JDBCConnector.class);
	
	private static String propertyFileName = "resource";
	
	private static Map<String, ComboPooledDataSource> DATA_SOURCES = new  HashMap<String, ComboPooledDataSource>();
	
	public static ComboPooledDataSource initDataSource(String dbName) {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		try {
			String url= PropertieUtil.getStrProp(propertyFileName, dbName + ".url");
			String user = PropertieUtil.getStrProp(propertyFileName, dbName + ".user");
			String pass = PropertieUtil.getStrProp(propertyFileName, dbName + ".password");
			if(url == null || user == null || pass == null) {
				return null;
			}
			dataSource.setDriverClass("com.mysql.jdbc.Driver");
			dataSource.setJdbcUrl(url);
			dataSource.setUser(user);
			dataSource.setPassword(pass);
			dataSource.setMinPoolSize(10);
			dataSource.setMaxPoolSize(40);
			dataSource.setInitialPoolSize(10);
			dataSource.setMaxIdleTime(60);
			dataSource.setAcquireIncrement(1);
			dataSource.setIdleConnectionTestPeriod(60);
			
			dataSource.setAutoCommitOnClose(true);
			dataSource.setPreferredTestQuery("SELECT NOW()");
		} catch (PropertyVetoException e) {
			e.printStackTrace();
			return null;
		}
		return dataSource;
	}

	public static Connection getConnection(String dbName) {
		try {
			ComboPooledDataSource db = DATA_SOURCES.get(dbName);
			if(db == null){
				db = initDataSource(dbName);
				DATA_SOURCES.put(dbName, db);
			}
			if(db == null) {
				log.debug("database["+dbName+"] has no info");
				System.out.println("database["+dbName+"] has no info");
				return null;
			}
			
//			return db.getConnectionPoolDataSource().getPooledConnection().getConnection();
			
			return db.getConnection();
		} catch (SQLException e) {
			System.out.println(e + dbName);
			e.printStackTrace();
			return null;
		}
	}


	public static Connection getConnection() {
		return getConnection("search_manager");
	}
	
	public static ComboPooledDataSource getDataSource() {
		ComboPooledDataSource ds = DATA_SOURCES.get("search_manager");
		if(ds == null) {
			getConnection("search_manager");
		}
		ds = DATA_SOURCES.get("search_manager");
		return ds;
	}
	
	public static void main(String[] args) throws SQLException {
		Connection c = getConnection("search_manager");
		System.out.println(c);
		c.close();
		int max = 50;
		int i = 1;
		Set<String> keys = new HashSet<>();
		while(max -- > 0) {
			c = getConnection("search_manager");
			String k = c.toString();
			if(keys.contains(k)) {
				System.out.println("exist");
			}
			keys.add(k);
			System.out.println(i + " : " + c);
			i ++;
			c.close();
		}
	}
}
